<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Suggest extends Model
{
    use HasFactory;

    protected $table = 'sp_suggest';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //status状态 1待处理  2处理中  3已处理  4已撤销  5已超时
    const SUGGEST_STATUS_NO_HANDLE = 1; //待处理
    const SUGGEST_STATUS_HANDLING = 2; //处理中
    const SUGGEST_STATUS_HANDLED = 3; //已处理
    const SUGGEST_STATUS_CANCEL = 4; //已撤销
    const SUGGEST_STATUS_OVERTIME = 5; //已超时

    //grade工单等级：1普通 2紧急 3严重
    const SUGGEST_GRADE_ORDINARY = 1; //普通
    const SUGGEST_GRADE_URGENT = 2; //紧急
    const SUGGEST_GRADE_SERIOUS = 3; //严重


    //添加
    protected function addData($data)
    {
        $default = array(
            'work_order' => rand(0, 1000) . time(), //工单号
            'status' => self::SUGGEST_STATUS_NO_HANDLE, //状态 1待处理  2处理中  3已处理  4已撤销  5已超时
            'create_time' => date("Y-m-d H:i:s"), //创建时间
            'update_time' => date('Y-m-d H:i:s'), //更新时间
        );
        $addData = array_merge($default, $data);
        return $data = $this->insertGetId($addData);
    }

    //根据id修改
    protected function editDataById($id, $data)
    {
        $where = array(
            'id' => $id,
        );
        return $data = $this->where($where)->update($data);
    }

    protected function GetOneSuggest($where)
    {
        $data = $this
            ->where($where)
            ->first();
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    //投诉列表
    protected function GetComplaintList($pageSize, $where, $grade_where)
    {
        $select = array('id', 'user_mobile', 'work_order', 'suggest_type', 'suggest_cate', 'suggest_title', 'grade', 'status', 'create_time', 'end_time');
        $data = $this
            ->select($select)
            ->where($where)
            ->where($grade_where)
            ->orderBy('id', 'desc')
            ->paginate($pageSize);
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //投诉工单等级数量
    protected function GetComplaintGradeCount($where, $grade_where)
    {
        $data = $this
            ->where($where)
            ->where($grade_where)
            ->count();
        return $data;
    }


}
